package main

import (
	"net"
	"fmt"
	"os"
	"io"
)

func main(){

	listenner,err := net.Listen("tcp","127.0.0.1:8000")
	if err != nil{
		fmt.Println("net.Listen err = ",err)
		return
	}

	defer listenner.Close()

	//阻塞等待用户连接
	conn,err1 := listenner.Accept()
	if err1 != nil{
		fmt.Println("listenner.Accept err = ",err)
		return
	}

	buf := make([]byte ,1024)
	var n int
	n,err = conn.Read(buf)

	fileName := string(buf[:n])

	conn.Write([]byte("ok"))

	//接收文件内容
	RecvFile(fileName,conn)
}

//接收文件内容
func RecvFile(fileName string,conn net.Conn){
	//新建文件
	f,err := os.Create(fileName)
	if err != nil{
		fmt.Println("os.Create err = ",err)
		return
	}

	buf := make([]byte,1024*4)

	// 接收多少，写多少
	for{
		n,err := conn.Read(buf)  //接收对方发来的文件内容
		if err != nil{
			if err == io.EOF{
				fmt.Println("接收完毕")
				return
			}
			fmt.Println("conn.Read err = ",err)
			return
		}

		if n == 0{
			fmt.Println("n=0文件接收完毕")
			return
		}

		f.Write(buf[:n])
	}
}